<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;

class TestController extends Controller
{
    /**
     * Home page
     * @param Request $request
     * @return View
     */
    public function index(Request $request)
    {
        $prj = $request->input('prj');

        //Get local branches
        $local_branches = $this->get_local_branches($prj);

        //Get remote branches
        $remote_branches = $this->get_remote_branches($prj);

        $branches = array(
            'local' => $local_branches,
            'remote' => $remote_branches
        );

        //Get current branch
        $current_branch = $this->get_current_branch($prj);

        //Get projects
        $projects = $this->get_projects();

        //dd($data);

        return view('test', array(
            'current_branch' => $current_branch,
            'local_branches' => $local_branches,
            'remote_branches' => $remote_branches,
            'prj' => $prj,
            'projects' => $projects
        ));
    }

    /**
     * Execute git command
     *
     * @return \Illuminate\Http\Response
     */
    public function cmd(Request $request)
    {

        $prj = $request->input('prj');
        $cmd = $request->input('cmd');
        $br = $request->input('br');
        $redirect = $request->input('redirect');

        $cmds = array(
            'checkout' => "git fetch;git checkout -b $br origin/$br",
            'delete' => "git branch -D $br",
            //'pull' => "git fetch;git merge origin/$br",
            'pull' => "git pull origin $br",
            'copy' => "git checkout $br;git checkout -b {$br}_". rand(1000, 9999) ." $br",
            'active' => "git checkout $br",
            'fetch' => 'git fetch'
        );

        if (!array_key_exists($cmd, $cmds)) {
            dd('cmd not found');
        }

        $path = env('WWW_ROOT', '/home/www');
        $git_cmd = $cmds[$cmd];

        $py_cmd = "../scripts/git_cmd.py $path $prj '$git_cmd'";
        //dd($py_cmd);
        exec($py_cmd, $results);


        if ($redirect == 'N') {
            echo '<pre>';
            print_r($results);

            return;
        }

        return response()->redirectTo('/?prj=' . $prj);
    }


    /**
     * Get remote branches
     * @param $prj
     * @return array
     */
    private function get_remote_branches($prj)
    {
        $path = env('WWW_ROOT', '/home/www');
        $cmd = 'git branch -rv';

        $py_cmd = "../scripts/git_cmd.py $path $prj '$cmd'";
        exec($py_cmd, $results);

        $remote_branches = array();
        foreach ($results as $line) {
            $clean_line = preg_replace("/[\s]+/is", " ", trim($line));
            $clean_line = str_replace('origin/', '', $clean_line);

            $branch_info = explode(' ', trim($clean_line), 3);
            $branch = array(
                'name' => $branch_info[0],
                'commit' => $branch_info[1],
                'message' => $branch_info[2]
            );
            array_push($remote_branches, $branch);
        }

        return $remote_branches;
    }

    /**
     * Get local branches
     * @param $prj
     * @return array
     */
    private function get_local_branches($prj)
    {
        $path = env('WWW_ROOT', '/home/www');
        $cmd = 'git branch -v';

        $py_cmd = "../scripts/git_cmd.py $path $prj '$cmd'";
        exec($py_cmd, $results);

        $local_branches = array();
        foreach ($results as $line) {
            $clean_line = preg_replace("/[\s]+/is", " ", trim($line));
            if (strpos($clean_line, '*') !== false) {
                $clean_line = str_replace('*', '', $clean_line);
            }

            $branch_info = explode(' ', trim($clean_line), 3);
            $branch = array(
                'name' => $branch_info[0],
                'commit' => $branch_info[1],
                'message' => $branch_info[2]
            );
            array_push($local_branches, $branch);
        }

        return $local_branches;
    }

    /**
     * Get current branch
     * @param $prj
     * @return null|string
     */
    private function get_current_branch($prj)
    {
        $path = env('WWW_ROOT', '/home/www');
        $cmd = 'git branch';

        $py_cmd = "../scripts/git_cmd.py $path $prj '$cmd'";
        exec($py_cmd, $results);

        $current_branch = null;
        foreach ($results as $line) {
            $clean_line = preg_replace("/[\s]+/is", " ", trim($line));
            if (strpos($clean_line, '*') !== false) {
                $clean_line = str_replace('*', '', $clean_line);
                $current_branch = trim($clean_line);
                break;
            }
        }

        return $current_branch;
    }

    /**
     * List projects
     * @return mixed
     */
    private function get_projects()
    {
        $path = env('WWW_ROOT', '/home/www');
        exec("../scripts/ll.py $path;", $results);

        return $results;
    }
}
